package com.douxg.fastCoding.enjoy;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.douxg.fastCoding.enjoy.source.MysqlEnjoySource;
import com.douxg.fastCoding.spring.consts.BizCode;
import com.douxg.fastCoding.spring.exception.BizRuntimeException;
import com.douxg.fastCoding.web.dto.EnjoyDto;
import com.douxg.fastCoding.web.entity.Enjoy;
import com.douxg.fastCoding.web.function.CodeAssistFunction;
import com.douxg.fastCoding.web.service.EnjoyService;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 基于mysql数据库的enjoy模板管理器
 * @Auther douxg
 * @Date 2020/5/12 23:32
 */
@Component
public class MysqlEnjoyManager implements EnjoyManager {
    private static Engine engine;
    @Resource
    private EnjoyService service;
    private static final Map<String, LocalDateTime> updateFlagMap = new ConcurrentHashMap<>();

    static {
        engine = Engine.use("code");
        if (engine == null) {
            engine = Engine.create("code");
            engine.setDevMode(true);
            engine.addSharedMethod(new StrKit());
            engine.addSharedObject("strUtil", new StrUtil());
            engine.addSharedObject("assist", new CodeAssistFunction());
        }
    }

    /**
     * 获取模板
     *
     * @param enjoyDto
     * @return
     */
    @Override
    public Template getTemplate(EnjoyDto enjoyDto) {
        Enjoy enjoy = service.selectOne(enjoyDto);
        if (enjoy == null || enjoy.getId() == null) {
            throw new BizRuntimeException().setBizCode(BizCode.ENJOY_NO_ERROR);
        }
        return getTemplateByContent(enjoy.getContent());
//        Template template = engine.getTemplate(source);
//        if (source.isModified() && source.getEnjoy().getUpdateTime() != null) {
//            updateFlagMap.put(source.getCacheKey(), source.getEnjoy().getUpdateTime());
//        }
//        return template;
    }

    /**
     * 获取模板
     *
     * @param content
     * @return
     */
    @Override
    public Template getTemplateByContent(String content) {
        return engine.getTemplateByString(content);
    }

    /**
     * 获取模板信息
     *
     * @param enjoyDto
     * @return
     */
    private MysqlEnjoySource getSource(EnjoyDto enjoyDto) {
        Enjoy enjoy = service.selectOne(enjoyDto);
        if (enjoy == null || enjoy.getId() == null) {
            throw new BizRuntimeException().setBizCode(BizCode.ENJOY_NO_ERROR);
        }
        BeanUtil.copyProperties(enjoy, enjoyDto);
        MysqlEnjoySource mysqlEnjoySource = new MysqlEnjoySource().setEnjoy(enjoy);
        boolean modify = true;
        if (enjoy.getUpdateTime() != null && enjoy.getUpdateTime().equals(updateFlagMap.get(mysqlEnjoySource.getCacheKey()))) {
            modify = false;
        }
        return mysqlEnjoySource.setModify(modify);
    }
}
